package ru.yermak.bookkeeping.currency;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import ru.yermak.bookkeeping.common.BookkeeperState;
import ru.yermak.bookkeeping.common.Command;
import ru.yermak.bookkeeping.common.UserDao;
import ru.yermak.bookkeeping.model.Currency;
import ru.yermak.bookkeeping.model.CurrencyCode;
import ru.yermak.bookkeeping.model.Group;
import ru.yermak.bookkeeping.ui.ModalForm;
import ru.yermak.bookkeeping.ui.Selector;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * User: harrier
 * Date: Aug 11, 2008
 */

@Scope("action")
@org.springframework.stereotype.Component
public class CurrencyAddCommand implements Command {

    @Autowired
    private UserDao userDao;
    @Autowired
    private BookkeeperState bookkeeperState;
    @Autowired
    private CurrencyDao currencyDao;
    @Autowired
    private ModalForm modalForm;
    @Autowired
    @CurrencyQulifier
    private Selector selector;

    @Autowired
    @CurrencyQulifier("addForm")
    private JComponent currencyAddForm;
    @Autowired
    @CurrencyQulifier("currencyBox")
    private JComboBox currencyBox;
    @Autowired
    @CurrencyQulifier("rate")
    private JTextField rateField;
    @Autowired
    @CurrencyQulifier("original")
    private JCheckBox originalCheckBox;

    @Transactional(propagation = Propagation.REQUIRED)
    public void execute(ActionEvent e) {
        List<Currency> existingCurrencies = currencyDao.findCurrencyByUserId(bookkeeperState.getUserId());
        List<CurrencyCode> existingCurrencyCodes = new ArrayList<CurrencyCode>();
        for (Currency existingCurrency : existingCurrencies) {
            existingCurrencyCodes.add(CurrencyCode.valueOf(existingCurrency.getCode()));
        }
        CurrencyCode[] codes = CurrencyCode.values();
        Collection availableCurrencyCodes = CollectionUtils.subtract(Arrays.asList(codes), existingCurrencyCodes);
        currencyBox.setModel(new DefaultComboBoxModel(availableCurrencyCodes.toArray()));

        boolean result = modalForm.show(currencyAddForm);
        if (result) {
            Currency currency = new Currency();
            currency.updateRate(new BigDecimal(rateField.getText()));
            //TODO map enum to hibernate by string
            currency.setCode(currencyBox.getSelectedItem().toString());
            //TODO resource Boundle
            currency.setName(currencyBox.getSelectedItem().toString());
            currency.setOriginal(originalCheckBox.isSelected());
            Group group = userDao.getGroupByUserId(bookkeeperState.getUserId());
            currency.setGroup(group);
            Integer currencyId = currencyDao.insertCurrency(currency);
            selector.rowInserted(currencyId);

        }
    }

}
